package io.zw.modules.xinpai.service.impl;

import io.zw.common.utils.MapUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.zw.common.utils.PageUtils;
import io.zw.common.utils.Query;

import io.zw.modules.xinpai.dao.UserProjectDao;
import io.zw.modules.xinpai.entity.UserProjectEntity;
import io.zw.modules.xinpai.service.UserProjectService;


@Service("userProjectService")
public class UserProjectServiceImpl extends ServiceImpl<UserProjectDao, UserProjectEntity> implements UserProjectService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<UserProjectEntity> page = this.selectPage(
                new Query<UserProjectEntity>(params).getPage(),
                new EntityWrapper<UserProjectEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveOrUpdate(Long userId, List<Long> areaList) {
        //先删除关系
        this.deleteByMap(new MapUtils().put("user_id", userId));
        if(areaList == null || areaList.size() == 0){
            return ;
        }
        //保存用户与项目区域的关系
        List<UserProjectEntity> list=new ArrayList<>(areaList.size());
        for (Long areaId : areaList) {
            UserProjectEntity userProjectEntity=new UserProjectEntity();
            userProjectEntity.setProjectId(areaId);
            userProjectEntity.setUserId(Long.valueOf(userId));
            list.add(userProjectEntity);
        }
        this.insertBatch(list);
    }

}
